#region  == Copyright == 
// =====================================================================
//  Microsoft Consulting Services France - Aymeric Mouillé - 2013
//  Projet     : CRM Solution Management Tool
//  Fichier    : MainForm.cs (30/04/2013 16:43)
//  
//  Copyright (C) Microsoft Corporation.  All rights reserved.
// =====================================================================
#endregion

namespace CRM.Solution.Management.Tool
{
    #region  == Using == 
    using System;
    using System.Windows.Forms;

    using Microsoft.Xrm.Client;
    using Microsoft.Xrm.Client.Services;
    using Microsoft.Xrm.Sdk;

    #endregion

    /// <summary>
    /// </summary>
    public partial class MainForm : Form
    {
        /// <summary>
        /// </summary>
        /// <param name="value">
        /// </param>
        /// <param name="progress">
        /// </param>
        private delegate void UpdateStatusDelegate(string value, int progress);

        #region Current Solution Import Job Id
        /// <summary>
        /// Gets or sets the current solution import job id.
        /// </summary>
        /// <value>
        /// The current solution import job id.
        /// </value>
        public Guid CurrentSolutionImportJobId { get; set; }
        #endregion

        #region Organization Service
        /// <summary>
        /// Gets the organization service.
        /// </summary>
        /// <value>
        /// The organization service.
        /// </value>
        public IOrganizationService OrganizationService { get; internal set; }
        #endregion

        #region = Constructor =
        /// <summary>
        /// Initializes a new instance of the <see cref="MainForm" /> class.
        /// </summary>
        public MainForm()
        {
            InitializeComponent();
        }
        #endregion

        #region Can Try Again
        /// <summary>
        /// Determines whether this instance [can try again] the specified can try again.
        /// </summary>
        /// <param name="canTryAgain">
        /// if set to <c>true</c> [can try again].
        /// </param>
        public void CanTryAgain(bool canTryAgain)
        {
            toolStripSplitButton.Visible = canTryAgain;
        }
        #endregion

        #region Get Organization Service
        /// <summary>
        /// Gets the organization service.
        /// </summary>
        /// <returns></returns>
        public IOrganizationService GetOrganizationService()
        {
            var connection = CrmConnection.Parse(settingsControl.GetCrmConnectionString());
            connection.Timeout = new TimeSpan(0, 5, 0);
            return new OrganizationService(connection);
        }
        #endregion

        #region Switch To Solution Import Progress
        /// <summary>
        /// </summary>
        public void SwitchToSolutionImportProgress()
        {
            tabControl.SelectTab("tabCurrentJob");
        }
        #endregion

        #region Update Strip Status
        /// <summary>
        /// Updates the strip status.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <param name="progress">The progress.</param>
        public void UpdateStripStatus(string message, int progress = -1)
        {
            if (InvokeRequired)
            {
                // We're not in the UI thread, so we need to call BeginInvoke
                BeginInvoke(new UpdateStatusDelegate(UpdateStripStatus), new object[] { message, progress });
                return;
            }

            // Must be on the UI thread if we've got this far
            toolStripStatusLabel.Text = message;
            if (progress == -1)
            {
                toolStripProgressBar.Style = ProgressBarStyle.Marquee;
            }
            else
            {
                toolStripProgressBar.Style = ProgressBarStyle.Blocks;
                toolStripProgressBar.Value = progress;
            }
            Application.DoEvents();
        }
        #endregion

        #region Main Form Load
        /// <summary>
        /// Handles the Load event of the MainForm control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">
        /// The <see cref="EventArgs" /> instance containing the event data.
        /// </param>
        private void MainForm_Load(object sender, EventArgs e)
        {
            if (!settingsControl.IsSettingsDefined())
            {
                tabControl.SelectTab("tabSettings");
                return;
            }

            OrganizationService = GetOrganizationService();
            solutionGridControl.LoadControl();
        }
        #endregion

        #region Tab Control Selected Index Changed
        /// <summary>
        /// </summary>
        /// <param name="sender">
        /// </param>
        /// <param name="e">
        /// </param>
        private void tabControl_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (tabControl.SelectedTab.Name == "tabAllJobs")
            {
                jobHistoryControl.LoadControl();
            }
            else if (tabControl.SelectedTab.Name == "tabSettings")
            {
                settingsControl.LoadControl();
            }
            else if (tabControl.SelectedTab.Name == "tabCurrentJob")
            {
                jobProgressControl.LoadControl();
            }
            else if (tabControl.SelectedTab.Name == "tabSolutions")
            {
                solutionGridControl.LoadControl();
            }
        }
        #endregion

        #region Tool Strip Split Button Button Click
        /// <summary>
        /// Handles the ButtonClick event of the toolStripSplitButton control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">
        /// The <see cref="EventArgs" /> instance containing the event data.
        /// </param>
        private void toolStripSplitButton_ButtonClick(object sender, EventArgs e)
        {
            CanTryAgain(false);
            if (tabControl.SelectedIndex == 0)
            {
            }
            else if (tabControl.SelectedIndex == 1)
            {
                jobHistoryControl.LoadControl();
            }
        }
        #endregion
    }
}
